//
//  MacrosPluginModule.m
//  Cantrip
//
//  Created by James Callender on Tue Jun 08 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "MacrosPluginModule.h"


@implementation MacrosPluginModule

- (id)init
{
    if (self = [super init]) {

    }
    
    return self;
}

- (NSString *)pluginName
{
    return @"MacrosPlugin";
}

- (NSString *)pluginVersion
{
    return @"0.1";
}

- (void)applicationDidLoadPlugin
{
    [CantripPluginManager() registerPlugin:self forService:InterfaceEventService priority:0];
}

- (BOOL)handleInterfaceEventAction:(NSEvent *)event
{
    id world = [[event window] world];
    id controller = [[event window] windowController];
    NSEnumerator *enumerate;
    NSDictionary *macro;

    if ([event type] != NSKeyDown) return NO;
    
    enumerate = [[[world worldData] objectForKey:@"macros"] objectEnumerator];
    while (macro = [enumerate nextObject]) {
	if ([macro objectForKey:@"enabled"] && 
	    [[macro objectForKey:@"keyCode"] intValue] == [event keyCode] && 
	    ([[macro objectForKey:@"modifierFlags"] intValue] & (NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask)) == 
		([event modifierFlags] & (NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask)))
	{
	    if ([macro objectForKey:@"script"] && ![[macro objectForKey:@"script"] isEqualToString:@""])
		[controller handleInput:[macro objectForKey:@"script"] withEnvironment:nil];
	    return YES;
	}
    }
    
    return NO;
}

@end
